﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace  RzCorportativo.Views.Cadastros
{
    public partial class Pacientes : BasePage
    {
        private object DataSource
        {
            get { return this.Session["$HistoricoPaciente$DataSource"]; }
            set { this.Session["$HistoricoPaciente$DataSource"] = value; }
        }

        //protected RzCorportativo.Lib.Consulta GetItem(object item)
        //{
            //if (!(item is RzCorportativo.Lib.Consulta))
            //    return new RzCorportativo.Lib.Consulta();
            //return item as RzCorportativo.Lib.Consulta;
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ListaConsulta.SetSystemPageSize();
            if (this.IsPostBack)
                return;
            if (this.Request.QueryString["paciente"] != null)
            {
                this.Paciente.LoadModel(new RzCorportativo.Lib.Usuario() { UsuarioId = EFramework.Web.TypeExtension.IntExtension.Parse(this.Request.QueryString["paciente"], 0) });
                //DataSource = RzCorportativo.Lib.Consulta.Listar(new RzCorportativo.Lib.Consulta()
                //{
                //    PacienteId = EFramework.Web.TypeExtension.IntExtension.Parse(this.Request.QueryString["paciente"], 0)
                //});
                //this.ListaConsulta.DataSource = this.DataSource;
                //this.ListaConsulta.DataBind();
            }
        }

        protected void ListaConsulta_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //if(this.DataSource == null && !string.IsNullOrEmpty(this.Request.QueryString["paciente"]))
            //    DataSource = RzCorportativo.Lib.Consulta.Listar(new RzCorportativo.Lib.Consulta()
            //    {
            //        PacienteId = EFramework.Web.TypeExtension.IntExtension.Parse(this.Request.QueryString["paciente"], 0)
            //    });
            //this.ListaConsulta.SetPage(e.NewPageIndex, DataSource);
        }

        protected void salvar_Click(object sender, EventArgs e)
        {
            //Lib.Paciente paciente = new RzCorportativo.Lib.Paciente();
            //paciente.Cpf = EFramework.Web.Configuration.MaskConfiguration.RemoveMask(this.Cpf.Text, "cpfKeyDel");
            //paciente.DataNascimento = EFramework.Web.TypeExtension.DateTimeExtension.Parse(this.DataNascimento.Text, DateTime.MinValue);
            //paciente.Email = this.Email.Text;
            //paciente.Nome = this.Nome.Text;
            //paciente.PacienteId = EFramework.Web.TypeExtension.IntExtension.Parse("", 0);
            //paciente.Sexo = this.Sexo.SelectedValue;
            //paciente.Telefone = EFramework.Web.Configuration.MaskConfiguration.RemoveMask(this.Telefone.Text, "telKeyDel");
            //paciente.Ativo = this.Ativo.Checked;
            RzCorportativo.Lib.Usuario paciente = new RzCorportativo.Lib.Usuario();
            paciente = this.Paciente.Model;
            Alert alert = new Alert("erroGeral", this);
            try
            {
                if (!this.Paciente.IsValid)
                {
                    var msgs = this.Paciente.GetErrors();
                    alert.AlertType = AlertType.Error;
                    int i = 0;
                    foreach (var item in msgs)
                    {
                        alert.Add(i.ToString(), item);
                        i += 1;
                    }
                    return;
                }
                bool result = paciente.Salvar();
                if (result)
                {
                    Session["op"] = "s";
                    this.ClientScript.RegisterStartupScript(this.GetType(), "error", "window.location='" + EFramework.Web.UrlBase.UrlPadrao + "Views/Consulta/Pacientes.aspx" + "'", true);
                }
            }
            catch (Exception ex)
            {
                alert.Add("exception", ex.Message);
            }
            finally
            {
                if (alert.HasMessage)
                {
                    alert.Show();
                }
            }
        }
    }
}